<template lang="pug">
el-dialog(title="商品分类" :visible.sync="dialogFormVisible" :before-close="close" append-to-body)
    el-form(:v-model="f" v-loading="loading" element-loading-text="数据提交中...")
        el-form-item(label="分类名称")
            el-input(v-model="f.name" auto-complete="off")
        el-form-item(label="排序")
            el-input(v-model="f.ord" auto-complete="off" type="number")
    .dialog-footer(slot="footer")
        el-button(@click="changestate") 取消
        el-button(type="primary" @click="save") 确定
</template>

<script>
    export default{
        data(){
            return{
                dialogFormVisible:false,
                f:{
                    name:'',
                    ord:0
                },
                gkid:-1,//id
                loading:false
            }
        },
        computed:{
            getstate(){
                return this.$store.state.goodkindeditclass;
            },
            getchange(){
                return this.$store.state.goodkindeditform;
            }
        },
        watch:{
            getstate(val){//打开弹窗
                this.dialogFormVisible=val
                // this.loading=false
            },//监视弹窗
            getchange(val){//获取到数据
                if(val!=undefined){
                    this.f.name=val.gcName
                    this.f.ord=val.gcSort
                    this.gkid=val.gcId
                }
            }//监视传参
        },
        methods:{
            changestate(){//打开弹窗
                this.$store.commit('ChangeGoodKindEditClass')
                this.loading=false
            },
            close(){//关闭,清空数据
                this.f={}
                this.$store.state.goodkindeditclass=false
            },
            save(){//验证,保存
                var _this=this
                if(this.f.name==""){
                    _this.$message({
                        type: 'info',
                        message: '分类名不能为空'
                    });
                    return
                }
                if(this.f.ord==""){
                    _this.$message({
                        type: 'info',
                        message: '排序不能为空'
                    });
                    return
                }
                this.loading=true
                if(this.gkid==-1){
                    var param = new URLSearchParams()
                    param.append("gcName",this.f.name)
                    param.append("gcSort",this.f.ord)
                    param.append("gcParentId","1")
                    param.append("gcPic","")
                    param.append("level","1")
                    axios.post('/saveXcxGoodsClass.do',param).then(r=>{
                        // console.log("新增",r)
                        if(r.data.code==200){
                            _this.$message({
                                type: 'success',
                                message: '新增已完成'
                            });
                        }else{
                            _this.$message.error(r.data.message)
                        }
                        _this.gkid=-1
                        _this.loading=false
                        _this.$store.commit('ChangeGoodKindEditClass')
                        _this.$store.state.goodkindnew=true
                    })
                }//当为新增
                else{
                    this.loading=true
                    var param = new URLSearchParams()
                    param.append("gcName",this.f.name)
                    param.append("gcSort",this.f.ord)
                    param.append("gcId",this.gkid)
                    axios.get('/updateByXcxGoodsClassId.do',{params:param}).then(r=>{
                        // console.log("修改",r)
                        if(r.data.code!=200){
                            _this.$message({
                                type:'info',
                                message:r.data.message
                            })
                            return
                        }
                        _this.$message({
                            type: 'success',
                            message: '修改已完成'
                        });
                        _this.$store.commit('ChangeGoodKindEditClass')
                        _this.loading=false
                        _this.$store.state.goodkindnew=true
                        _this.gkid=-1
                    })
                }//当前为修改编辑
                
            }
        }
    }
</script>
<style>
.el-dialog__header {
    padding: 10px;
    background-color: #20a0ff!important;
}
</style>